from unittest.mock import MagicMock
from unittest.mock import patch

# mockowanie (lub stubowanie) klas tworzenie obiektów tych klas z
# częściowo zmienionym zachowaniem.

#example 1 MagicMock

class ProductionClass:
     def method(self):
        self.something(1, 2, 3)
     def something(self, a, b, c):
         pass

real           = ProductionClass()
real.something = MagicMock()
real.method()
real.something.assert_called_once_with(1, 2, 3)

#example 2 Mock WITH, contecst manager

arg = 100

def func(x):

     global arg
     arg = x
     return arg

print (arg)

with patch('__main__.func') as mock_func:

    #Mock objects remember how they have
    #been called and you can test that they were called correctly.
    func(10)
    mock_func.assert_called_with(10)
    func(20)
    mock_func.assert_called_with(20)

    #No matter how it is called it will return 42
    mock_func.return_value = 42
    print (func(300))
    print (func(350))

print (arg)

#example 3 Mock as Patch

def function(x):
     return x

#@patch('__main__.function') <=> unitTest1 = patch('__main__.function')(unitTest1)
@patch('__main__.function')
def unitTest1(mock):
    print (mock)
     
unitTest1()  



